package com.igeek.boot.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.boot.entity.Orders;
import com.igeek.boot.entity.Ordersetting;
import com.igeek.boot.mapper.OrdersettingMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-23
 */
@DS("settings")
@Service
public class OrdersettingServiceImpl extends ServiceImpl<OrdersettingMapper, Ordersetting> implements OrdersettingService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;

    @Override
    //若在执行当前方法时有事务正在工作，将这个外部事务进行挂起，只运行本方法内部的事务
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public boolean updateByDate(Orders order) {
        //2.根据预约日期 来更新 已预约人数+1
        LocalDate orderDate = order.getOrderDate();
        Ordersetting ordersetting =
                ordersettingMapper.selectOne(
                        new LambdaQueryWrapper<Ordersetting>().eq(Ordersetting::getOrderDate, orderDate));
        ordersetting.setReservations(ordersetting.getReservations()+1);
        int row2 = ordersettingMapper.updateById(ordersetting);
        return row2>0;
    }
}
